package com.louis.buddhist.widget.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.louis.buddhist.R;
import com.louis.buddhist.data.C;
import com.louis.buddhist.data.entity.CatalogEntity;
import com.louis.buddhist.ui.adapter.CatalogDialogAdapter;

import java.util.ArrayList;

public class CatalogDialogFragment extends DialogFragment {


    private static final String TAG = "CatalogDialogFragment";
    private View mView;
    private RecyclerView mRecyclerView;
    private ArrayList<CatalogEntity.EbookContentBean> mCatalogList;
    private CatalogDialogAdapter mAdapter;
    private PageToCatalogListener mListener;

    public void setmListener(PageToCatalogListener mListener) {
        this.mListener = mListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.dialog_catalog, container, false);
        //启用窗体的扩展特性。
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setCancelable(false);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        initView();
        return mView;
    }

    private void initView() {

        ImageView close = mView.findViewById(R.id.iv_close);
        mRecyclerView = mView.findViewById(R.id.rv_list);

        Bundle arguments = getArguments();
        if (arguments != null) {
            mCatalogList = arguments.getParcelableArrayList(C.IntentKey.MESSAGE_INTENT_KEY);
        }

        initRecyclerView();

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initRecyclerView() {

        if (mCatalogList != null && mCatalogList.size() > 0) {
            mAdapter = new CatalogDialogAdapter(R.layout.adapter_catalog_list, mCatalogList);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

            mRecyclerView.setLayoutManager(linearLayoutManager);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    CatalogEntity.EbookContentBean bean = (CatalogEntity.EbookContentBean) adapter.getData().get(position);
                    if(mListener != null){
                        mListener.toPage(bean);
                    }
                    dismiss();
                }
            });
            mRecyclerView.setAdapter(mAdapter);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    public interface PageToCatalogListener{

        void toPage(CatalogEntity.EbookContentBean bean);
    }
}